 // pages/index/zone/goodsDetail/appraise/publish/.publish.js
import { axios } from '../../../../utils/ajax.js'
let app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    uploadimg: [],
    data: '',
    userid: '',
    token: '',
    orderno:'',
    //投诉类型集合
    complainbox:[{
      name: '发货速度',
      checked: false
    }, {
      name: '物流速度',
      checked: false
      }, {
        name: '退款速度',
        checked: false
    }, {
        name: '退货速度',
      checked: false
      }, {
        name: 'App界面设计',
        checked: false
    }, {
        name: '客户服务质量',
      checked: false
      }, {
        name: '商品质量',
        checked: false
      }],
    //传给后台的type
    complaintype: [],
    //是否必须传图片
    mustimg:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let { userid = '', token = '' } = wx.getStorageSync('userinfo')
    this.setData({
      userid: userid,
      token: token,
      orderno: options.orderno
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //提交按钮
  confirmmeg(e) {
    let complainbox = this.data.complainbox
    this.setData({
      mustimg: complainbox[6].checked?true:false
    })
    let usercontent = e.detail.value.usercontent
    let data = this.data.data
    let { userid = '', token = '' } = wx.getStorageSync('userinfo')
    //投诉类型没选择
    if (!this.data.complainbox.find(x=>x.checked ==true)) {
      wx.showToast({
        title: '请选择投诉类型', 
        icon: "none"
      })
      //评论没写
    } else if (!usercontent) {
      wx.showToast({
        title: '请填写意见反馈', 
        icon: "none"
      })
    //要求传图片 且图片没传
    } else if (this.data.mustimg && this.data.uploadimg.length == 0){
      wx.showToast({
        title: '请上传图片',
        icon: "none"
      })
    //控制最大传三张图片
    } else if (this.data.uploadimg.length>3) {
      wx.showToast({
        title: '最多上传三张图片',
        icon: "none"
      })
    }else {
      let time = new Date().getTime() / 1000
      let complainbox = this.data.complainbox
      let complaintype = []
      complainbox.forEach((value, index) => {
        if (value.checked) {
          complaintype.push(index + 1)
        }
      })
      this.setData({
        complaintype: complaintype.join(',')
      })
      console.log(this.data.orderno, usercontent, this.data.complaintype, this.data.uploadimg.length)
      axios({
        url: `/order/Complaint/token/${this.data.token}/userid/${this.data.userid}`,
        method: "POST",
        data: {
          orderNo: this.data.orderno,
          content: usercontent,
          type: this.data.complaintype,   //1-7的序列号
          isImgs: this.data.uploadimg.length?1:0
        }
      }).then((res)=>{
        console.log(res)
        console.log(res.data.complaintId)
        wx.showToast({
          title: res.data.message,
          icon:"none"
        })
        if (this.data.uploadimg.length){
          this.data.uploadimg.forEach((val,index)=>{
            wx.uploadFile({
              url: `${app.host}/order/publishComplaintImg/token/${this.data.token}/userid/${this.data.userid}`,
              filePath: val,
              name: 'complainfiles',
              formData: {
                isLast: index == this.data.uploadimg.length-1?1:0,
                complaintId: res.data.complaintId
              },
              success: function (res) {
                console.log(res)
                let data = JSON.parse(res.data)
                console.log(data)
                if (data.result == 1) {
                  wx.showToast({
                    title: data.message,
                    icon: "none"
                  })
                  setTimeout(() => {
                    wx.navigateBack({
                      delta: 1
                    })
                  }, 2000)
                } else {
                  wx.showToast({
                    title: data.message,
                    icon: "none"
                  })
                }
              }
            })
          })
        }else{
          setTimeout(() => {
            wx.navigateBack({
              delta: 1
            })
          }, 2000)
        }
      })
    }
  },
  //上传图片
  uploadimg() {
    wx.chooseImage({
      count: app.globalData.maxuploadNumber, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: (res) => {
        console.log(res)
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        let obj = this.data.uploadimg
        res.tempFilePaths.forEach((val) => {
          obj.push(val)
        })
        this.setData({
          uploadimg: obj
        })
      }
    })
  },
  //选择投诉类型
  choose (e) {
    let index =e.currentTarget.dataset.index
    let complainbox = this.data.complainbox
    complainbox[index].checked = !complainbox[index].checked
    this.setData({
      complainbox: complainbox
    })
  }
})